setwd("~/accept_decline")

mean_impute = function(x)
{
	mu = mean(na.omit(x))
	x[is.na(x)] = mu
	x
}

df = readRDS("train.rds")
df2 = data.frame(lapply(df, mean_impute))[,-1]

v1 = sort(sapply(df2[,1:778],function(x) mean(df2$loss[x >= quantile(x, .999)]>0)), decr=T)[1:15]
v2 = sort(sapply(df2[,1:778],function(x) mean(df2$loss[x <= quantile(x, .001)]>0)), decr=T)[1:15]

vars1 = paste("df2", names(v1), sep="$")
vars2 = paste("df2", names(v2), sep="$")

breaks1 = sapply(vars1, function(txt) quantile(eval(parse(text=txt)), .999))
breaks2 = sapply(vars1, function(txt) quantile(eval(parse(text=txt)), .001))

df_tail = list()
for (i in 1:length(vars1)) df_tail[[i]] = as.integer(eval(parse(text=vars1[i])) > breaks1[i])
for (i in 1:length(vars2)) df_tail[[i + length(vars1)]] = as.integer(eval(parse(text=vars2[i])) < breaks2[i])

df_tail[[length(vars1) + length(vars2) + 1]] = as.integer(df2$loss > 0)
df_tail = as.data.frame(df_tail)
names(df_tail) = c(names(v1), names(v2), "loss_ind")

m = glm(loss_ind ~ f471 + f468 + f536 + f369 + f161 + f471*f468, data=df_tail, family=binomial)


